//Arduino UNO 上的 RX、TX 分别对接 Syn6288 上的 TX、RX
#include <SoftwareSerial.h>
#include "Syn6288.h"
#include <ArduinoJson.h>
Syn6288 syn;
SoftwareSerial softSerial1(2,3); //RX = 2，TX = 3

// 不同的语音种类，后期可以实现对语音的个性化定制
uint8_t text1[] = {0xC7, 0xEB, 0xCE, 0xF0, 0xD4, 0xDA, 0xB4, 0xCB,0XCE,0XFC,0XD1,0XCC}; //请勿在此吸烟
uint8_t text21[] = {0xCA,0xD2,0xC4,0xDA,0xCE,0xFC,0xD1,0xCC,0xCA,0xC7,0xB6,0xD4};
uint8_t text22[] = {0xCB,0xFB,0xC8,0xCB,0xBD,0xA1,0xBF,0xB5,0xB5,0xC4,0xC9,0xCB,0xBA,0xA6};//吸烟是对他人健康的危害   
//这里是因为text过长，需要变成两段语音输出                
uint8_t text3[] = {0xCA,0xD2,0xC4,0xDA,0xCE,0xFC,0xD1,0xCC,0xB4,0xA5,0xB7,0xB8,0xB7,0xA8,0xC2,0xC9};//此处吸烟触犯法律
//uint8_t text7[] = {0xC4, 0xE3, 0xC2, 0xE8, 0xCB, 0xC0, 0xC0, 0xD6}; 

int switch_get=0;
int type=0;
//获取sep8266上发送过来的数据
String json(){
  String comdata = "";
   while(softSerial1.available() > 0){//串口接收到数据
        comdata += char(softSerial1.read());//获取串口接收到的数据
          delay(2);
    }
    Serial.print(comdata);
  return comdata;
  
}

void setup()
{
  Serial.begin(9600); // 初始化串口通信:
  softSerial1.begin(9600); //初始化软串口通信；
  softSerial1.listen(); //监听软串口通信
  

}
void loop()
{

// 接受串口信息的代码，将串口的json信息赋值给switch和control两变量
  String jsonData= json();
      if(jsonData.length()>0){
          const size_t capacity = JSON_OBJECT_SIZE(1) + 10;
       DynamicJsonDocument doc(capacity);
       deserializeJson(doc, jsonData);
       const char* root_switch = doc["switch"]; 
       const char* root_ytpe = doc["control"];// "0"
       int numKey=doc["switch"].as<int>();
       int controlKey=doc["control"].as<int>();
       Serial.println("获取到的数据：");
       Serial.println(numKey);
       Serial.println(controlKey);
       switch_get=numKey;
       type=controlKey;
       delay(100);
        }else{
          //不做操作
           delay(1000);  }      



// 进行判断播放音频
  if(switch_get==1)
  {
    if(type==1){syn.play(text1, sizeof(text1), 1);
    Serial.println("开始播放1");};
    if(type==2){syn.play(text21, sizeof(text21), 1);
    syn.play(text22, sizeof(text22), 1);};
    if(type==3)syn.play(text3, sizeof(text3), 1);
  }
  else{
    delay(10);
  }


}

